<?php
/**
 * 后台基类
 * @author yupoxiong<i@yupoxiong.com>
 */

declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\model\AdminMenu;
use app\admin\model\AdminUser;
use app\admin\traits\AdminAuthTrait;
use app\admin\traits\AdminPhpOffice;
use app\admin\traits\AdminTreeTrait;
use Exception;

//require_once '../vendor/autoload.php';
// 导入对应产品模块的client
// 导入要请求接口对应的Request类
use think\facade\Env;
// 导入可选配置类
use think\response\Json;
use think\View;

class AdminBaseController {
	// 引入树相关trait
	use AdminTreeTrait;

	// 引入权限判断相关trait
	use AdminAuthTrait;

	// 引入office相关trait
	use AdminPhpOffice;

	/**
	 * 后台主变量
	 * @var array
	 */
	protected array $admin;

	/**
	 * 视图变量
	 * @var View
	 */
	protected View $view;

	/**
	 * 当前访问的URL
	 * @var string
	 */
	protected string $url;

	/**
	 * 当前访问的菜单
	 * @var mixed
	 */
	protected $menu;

	/**
	 * 无需验证登录的url
	 * @var array
	 */
	protected array $loginExcept = [];

	/**
	 * 无需验证权限的URL
	 * @var array
	 */
	protected array $authExcept = [
		'admin/error/err403',
		'admin/error/err404',
		'admin/error/err500',
	];

	/**
	 * 当前后台用户
	 * @var AdminUser
	 */
	protected AdminUser $user;

	public function __construct() {
		// 初始化
		$this->initialize();
	}

	/**
	 * 初始化方法
	 */
	public function initialize(): void {
		// 检查登录
		$this->checkLogin();
		// 检查权限
		$this->checkAuth();
		// 单设备登录检查
		$this->checkOneDeviceLogin();
		// csrfToken检查
		$this->checkToken();

		// 初始化view
		$this->view = app()->make(View::class);

		// 分页每页数量
		$this->admin['admin_list_rows'] = cookie('admin_list_rows') ?? 10;
		// 限制每页数量最多不超过100
		$this->admin['admin_list_rows'] = $this->admin['admin_list_rows'] < 100 ? $this->admin['admin_list_rows'] : 100;
		/** @var AdminMenu $menu */
		$this->menu = (new AdminMenu)->where(['url' => $this->url])->findOrEmpty();

		if (isset($this->user) && !$this->menu->isEmpty() && request()->method() === $this->menu->log_method) {
			// 如果用户登录了而且符合菜单记录日志方式，记录操作日志
			$this->createLog($this->user, $this->menu->name);
		}
	}

	/**
	 * 模板赋值
	 * @param $name
	 * @param null $value
	 * @return View
	 */
	protected function assign($name, $value = null): View {
		return $this->view->assign($name, $value);
	}

	/**
	 * 渲染模板
	 * @param string $template
	 * @param array $vars
	 * @return string
	 * @throws Exception
	 */
	protected function fetch(string $template = '', array $vars = []): string {
		// 顶部导航
		$this->admin['top_nav'] = (int) setting('admin.display.top_nav');
		// 后台基本信息配置
		$this->admin['base'] = setting('admin.base');
		// 当前顶部导航ID
		$current_top_id = 0;

		if (!$this->menu->isEmpty()) {
			$menu = $this->menu;
			$menu_all = (new AdminMenu)->field('id,parent_id,name,icon')->select()->toArray();
			// 当前页面标题
			$this->admin['title'] = $menu->name;
			// 当前面包屑
			$this->admin['breadcrumb'] = $this->getBreadCrumb($menu_all, $menu->id);
			if ($this->admin['top_nav'] === 1) {
				// 顶部导航id
				$current_top_id = $this->getTopParentIdById($menu_all, $menu->id);
			}
		}
		// 当前是否为pjax访问
		$this->admin['is_pjax'] = request()->isPjax();
		// 上传文件url
		$this->admin['upload_url'] = url('admin/file/upload')->build();
		// 退出url
		$this->admin['logout_url'] = url('admin/auth/logout')->build();

		if ('admin/auth/login' !== $this->url && !$this->admin['is_pjax'] && 'admin/project_log/template' !== $this->url) {
			// 展示菜单
			$show_menu = $this->user->getShowMenu($this->admin['top_nav']);
			// 顶部导航
			$this->admin['top_menu'] = $show_menu['top'];
			// 左侧菜单
			$this->admin['left_menu'] = $this->getLeftMenu($show_menu['left'][$current_top_id], $menu->id ?? 0);
		}
		// 是否开启debug
		$this->admin['debug'] = Env::get('app_debug') ? 1 : 0;
		// 顶部导航
		$this->admin['top_nav'] = 1;
		// 顶部搜索
		$this->admin['top_search'] = 0;
		// 顶部消息
		$this->admin['top_message'] = 0;
		// 顶部通知
		$this->admin['top_notification'] = 0;
		// 文件删除url
		$this->admin['file_del_url'] = url('admin/file/del');
		// 地图配置
		$this->admin['map'] = config('map');
		// 当前用户
		$this->admin['user'] = $this->user ?? new AdminUser();

		// 赋值后台变量
		$this->assign([
			'admin' => $this->admin,
		]);
		return $this->view->fetch($template, $vars);
	}

	/**
	 * 访问不存在的方法
	 * @param $name
	 * @param $arguments
	 * @return string|Json
	 * @throws Exception
	 */
	public function __call($name, $arguments) {
		if (request()->isPost()) {
			return admin_error('页面未找到');
		}
		return $this->fetch('error/404');
	}

	public function send_sms($template_id, $param, $mobile) {
		try {

			$cred = new Credential("AKIDJTP8kHagXEi17zf6PUL4c4KRREmRzLUS", "Ii0BHOSQSPAsRtGUZZP3WwLpIerBTwc8");

			$httpProfile = new HttpProfile();

			$httpProfile->setReqMethod("GET"); // post请求(默认为post请求)
			$httpProfile->setReqTimeout(30); // 请求超时时间，单位为秒(默认60秒)
			$httpProfile->setEndpoint("sms.tencentcloudapi.com"); // 指定接入地域域名(默认就近接入)

			$clientProfile = new ClientProfile();
			$clientProfile->setSignMethod("TC3-HMAC-SHA256"); // 指定签名算法(默认为HmacSHA256)
			$clientProfile->setHttpProfile($httpProfile);

			$client = new SmsClient($cred, "ap-nanjing", $clientProfile);

			// 实例化一个 sms 发送短信请求对象,每个接口都会对应一个request对象。
			$req = new SendSmsRequest();

			/* 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId，示例如1400006666 */
			// 应用 ID 可前往 [短信控制台](https://console.cloud.tencent.com/smsv2/app-manage) 查看
			$req->SmsSdkAppId = "1400880328";

			$req->SignName = "徐州恒清食品科技有限公司";

			$req->TemplateId = $template_id;

			$req->TemplateParamSet = $param;

			$req->PhoneNumberSet = $mobile;

			$resp = $client->SendSms($req);

			$result = $resp;
			return $result;

		} catch (TencentCloudSDKException $e) {
			//echo $e;
		}

	}
}
